/*
 * motor_control.h
 *
 *  Created on: Nov 4, 2024
 *      Author: ydl43
 */

#ifndef MOTOR_CONTROL_INC_MOTOR_CONTROL_H_
#define MOTOR_CONTROL_INC_MOTOR_CONTROL_H_


#include "stm32h7xx_hal.h"

//=======================================================================Parameter=======================================================================
#define REDUCTION_RATIO             ((uint8_t)20)      //电机减速比
#define SAMPLE_FREQUENCY            ((double)1000)     //电机转速采样频率
#define CIRCLE_LINES                ((uint8_t)13)      //编码器线数
#define FREQUENCY_MULTIPLICATION    ((uint8_t)4)       //倍频数
#define STOP_ROTATION               ((uint8_t)0)       //停转
#define FORWARD_ROTATION            ((uint8_t)1)       //正转
#define ROLLBACK_ROTATION           ((uint8_t)2)       //反转
#define NO_OVERFLOW                 ((uint8_t)0)       //无溢出
#define FORWARD_OVERFLOW            ((uint8_t)1)       //正转溢出
#define ROLLBACK_OVERFLOW           ((uint8_t)2)       //反转溢出
#define MAX_SPEED                   ((double)70)       //最大速度
//=======================================================================================================================================================


//=================================================================Motor State Structure=================================================================
typedef struct{
	uint32_t          Counter_Value;
	uint32_t          last_Counter_Value;
	double            speed;
	double            angle;
	uint8_t           Dirction;
	GPIO_TypeDef     *IN1_Port;
	uint16_t          IN1_Pin;
	GPIO_TypeDef     *IN2_Port;
	uint16_t          IN2_Pin;
	double            target_PWM;
	double            target_Speed;
	uint8_t           overflow;
}Motor;
//=======================================================================================================================================================


//=====================================================================PID Structure=====================================================================
typedef struct{
	double P;
	double I;
	double D;
	double Error;
	double Last_Error;
	double Last_2_Error;
	double sum_Error;
}PID;
//=======================================================================================================================================================


//==================================================================Functions Definition=================================================================
void Motor_Speed_Get(Motor *motor, TIM_HandleTypeDef *htim);
void Motor_Angle_Get(Motor *motor, TIM_HandleTypeDef *htim);
void Motor_Direction_Control(Motor *motor, uint8_t dir);
void Motor_Speed_Control(Motor *motor, PID *pid, TIM_HandleTypeDef *htim, uint32_t channel, double target_Speed);
void Motor_Angle_Control(Motor *motor, PID *pid_Angle, PID *pid_Speed, TIM_HandleTypeDef *htim, uint32_t channel, double target_Angle);
//=======================================================================================================================================================


//=====================================================================Use Examples======================================================================
//
//|-----------------------------------------------------------------------------------------------------------------------------------------------------|
//|		Timer		|	Channel		|	Function				|	PSC		|	ARR		|	Frequency(Hz)	|	Preemption Priority	|	Sub Priority	|
//|-----------------------------------------------------------------------------------------------------------------------------------------------------|
//|		Timer 13	|	Channel 1	|	PWM Wave Generates		|	9		|	9999	|	1000			|	--					|	--				|
//|		Timer 5		|	---------	|	Set Control Interval	|	999		|	999		|	100				|	2					|	0				|
//|		Timer 6		|	---------	|	Set Collect Interval	|	99		|	999		|	1000			|	1					|	0				|
//|		Timer 4		|	---------	|	Read Encoder			|	0		|	40000	|	----			|	0					|	0				|
//|-----------------------------------------------------------------------------------------------------------------------------------------------------|
//
//-----------------------------------Explanation: Realize cascade PID control of the angular displacement of the motor.----------------------------------
//
//-------------------------------------------------------------------------Code--------------------------------------------------------------------------
//
///* USER CODE BEGIN 4 */
//void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
//{
//	if (htim->Instance==htim4.Instance)		             //Encoder overflow interrupts                  Priority 0
//	{
//		if (motor_D.Dirction == FORWARD_ROTATION) motor_D.overflow = FORWARD_OVERFLOW;
//		else if (motor_D.Dirction == ROLLBACK_ROTATION) motor_D.overflow = ROLLBACK_OVERFLOW;
//		else motor_D.overflow = NO_OVERFLOW;
//	}
//	if (htim->Instance==htim6.Instance)                  //Speed test interval interrupts    1000Hz     Priority 1
//	{
//		Motor_Angle_Get(&motor_D, &htim4);
//	}
//	if (htim->Instance==htim5.Instance)                  //Speed control interrupts          100Hz      Priority 2
//	{
//		Motor_Angle_Control(&motor_D, &pid_D_Angle, &pid_D_Speed, &htim13, TIM_CHANNEL_1, target_Angle);
//	}
//}
///* USER CODE END 4 */
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------Motor Initialize-------------------------------------------------------------------
//
//Motor motor_D = {0,0,0,0,STOP_ROTATION,DIN1_GPIO_Port,DIN1_Pin,DIN2_GPIO_Port,DIN2_Pin,0,0,0};
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------PID Parameter Example-----------------------------------------------------------------
//
//PID pid_D_Speed = {2,0.01,0,0,0,0,0};
//PID pid_D_Angle = {10,1,0.5,0,0,0,0};
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------
//=======================================================================================================================================================

#endif /* MOTOR_CONTROL_INC_MOTOR_CONTROL_H_ */
